package com.sansan.qiangji.realize.warehouse;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.sansan.qiangji.VO.warehouse.purchaseOrder.PurchaseOrderListResultVO;
import com.sansan.qiangji.VO.warehouse.purchaseOrder.PurchaseOrderVO;
import com.sansan.qiangji.entity.warehouse.PurchaseOrder;
import com.sansan.qiangji.entity.warehouse.example.PurchaseOrderExample;
import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;

import com.sansan.qiangji.form.warehouse.PurchaseOrderAddForm;
import com.sansan.qiangji.form.warehouse.PurchaseOrderEditForm;
import com.sansan.qiangji.form.warehouse.PurchaseOrderListForm;
import com.sansan.qiangji.service.inter.warehouse.PurchaseOrderService;
import com.sansan.qiangji.utils.CopyUtil;
import com.sansan.qiangji.utils.StringUtil;
import com.sansan.qiangji.utils.pageUtils.PageResult;
import com.sansan.qiangji.utils.pageUtils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class PurchaseOrderReal {
    @Autowired
    private PurchaseOrderService purchaseOrderService;


    @Transactional
    public void deletePurchaseOrder(Integer purchaseOrderId){
        PurchaseOrder purchaseOrder = purchaseOrderService.selectByPrimaryKey(purchaseOrderId);
        if(purchaseOrder==null){
            log.error("【设备删除】设备不存在，purchaseOrderId={}",purchaseOrderId);
            throw new ServiceException(ResultEnum.PURCHASE_ORDER_IS_NOT_EXIST);
        }
        int row = purchaseOrderService.deleteByPrimaryKey(purchaseOrderId);
        if(row==0){
            log.error("【设备删除】设备删除失败，purchaseOrderId={}",purchaseOrderId);
            throw new ServiceException(ResultEnum.PURCHASE_ORDER_DELETE_ERROR);
        }
    }

    @Transactional
    public void addPurchaseOrder(PurchaseOrderAddForm purchaseOrderAddForm){
        PurchaseOrderExample purchaseOrderExample = new PurchaseOrderExample();

        PurchaseOrder purchaseOrder = new PurchaseOrder();
        BeanUtils.copyProperties(purchaseOrderAddForm,purchaseOrder);
        String purchaseOrderCode = StringUtil.codeGenerater("purchase_order");
        //计算总工资
        purchaseOrder.setTotalPrice(purchaseOrderAddForm.getPrice()*purchaseOrderAddForm.getAmount());
        purchaseOrder.setCreateTime(new Date());
        purchaseOrder.setPurchaseOrderCode(purchaseOrderCode);
        int row = purchaseOrderService.insert(purchaseOrder);
        if(row==0){
            log.error("【设备新增】设备新增失败");
            throw new ServiceException(ResultEnum.PURCHASE_ORDER_INSERT_ERROR);
        }
    }



    @Transactional
    public void editPurchaseOrder(PurchaseOrderEditForm purchaseOrderEditForm){
        PurchaseOrder purchaseOrder = purchaseOrderService.selectByPrimaryKey(purchaseOrderEditForm.getId());
        if (purchaseOrder == null){
            log.error("【编辑采购单】采购单不存在，purchaseOrderEditForm={}",purchaseOrderEditForm.toString());
            throw new ServiceException(ResultEnum.PURCHASE_ORDER_IS_NOT_EXIST);
        }
        if("已采购".equals(purchaseOrder.getOrderStatus())){
            log.error("【编辑采购单】已采购不可编辑，purchaseOrderEditForm={}",purchaseOrderEditForm.toString());
            throw new ServiceException(ResultEnum.PURCHASE_ORDER_NOT_ALLOW_EDIT_ERROR);
        }

        CopyUtil.copyPropertiesNotNull(purchaseOrderEditForm,purchaseOrder);
        purchaseOrder.setTotalPrice(purchaseOrderEditForm.getPrice()*purchaseOrderEditForm.getAmount());
        int row = purchaseOrderService.updateByPrimaryKey(purchaseOrder);
        if(row==0){
            log.error("【采购单新增】采购单新增失败");
            throw new ServiceException(ResultEnum.PURCHASE_ORDER_INSERT_ERROR);
        }
    }


    public PurchaseOrderListResultVO purchaseOrderList(PurchaseOrderListForm purchaseOrderListForm){
        PurchaseOrderExample purchaseOrderExample = new PurchaseOrderExample();
        int pageNum = purchaseOrderListForm.getPageNum();
        int pageSize = purchaseOrderListForm.getPageSize();
        PageHelper.startPage(pageNum,pageSize);
        PurchaseOrderExample.Criteria criteria = purchaseOrderExample.createCriteria();

        List<PurchaseOrder> purchaseOrderList = purchaseOrderService.selectByExample(purchaseOrderExample);
        PageResult<PurchaseOrder> purchaseOrderPageResult = PageUtil.getPageResult(new PageInfo<>(purchaseOrderList));
        PurchaseOrderListResultVO purchaseOrderListResultVO = new PurchaseOrderListResultVO();
        BeanUtils.copyProperties(purchaseOrderPageResult,purchaseOrderListResultVO);
        List<PurchaseOrderVO> purchaseOrderVOList = new ArrayList<>();
        for(PurchaseOrder purchaseOrder : purchaseOrderPageResult.getContent()){
            PurchaseOrderVO purchaseOrderVO = new PurchaseOrderVO();
            BeanUtils.copyProperties(purchaseOrder,purchaseOrderVO);
            purchaseOrderVOList.add(purchaseOrderVO);
        }
        purchaseOrderListResultVO.setPurchaseOrderVOS(purchaseOrderVOList);
        return purchaseOrderListResultVO;
    }

}
